package com.sn.author.service.dao;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sn.author.domain.BaseResult;
import com.sn.author.domain.vo.PageVo;
import com.sn.author.filter.content.SecurityContextHolder;
import com.sn.author.utils.DateUtils;
import com.sn.author.utils.ReflectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author xiaom
 * @Date 2020/9/16 14:50
 * @Version 1.0.0
 * @Description <>
 **/
public interface BaseService {

    /**
     * Set global default attributes
     * @param <T> source
     * @return source
     */
    default <T> T injectDefaultAttr(T source){
        ReflectionUtils.setFieldValue(source,"importTime", DateUtils.formatYMDHMS(new Date()));
        ReflectionUtils.setFieldValue(source,"importUser", SecurityContextHolder.getCurrentUserName());
        ReflectionUtils.setFieldValue(source,"importUserId",SecurityContextHolder.getCurrentUserId());
        return source;
    }

    /**
     * Default page setting
     */
    default <T extends PageVo> void startPage(T t){
        Integer page = t.getPageIndex();
        Integer pageSize = t.getPageSize();
        page = (page == null) ? 1 :page;
        pageSize = (pageSize == null) ? 20 : pageSize;
        PageHelper.startPage(page,pageSize);
    }
}
